/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.action;

import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.util.Map;

import javacommon.base.BaseStruts3Action;
import javacommon.util.ResInfo;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.pub.model.PubCcsq;
import com.tech15.pub.service.PubCcsqManager;
import com.tech15.pub.vo.query.PubCcsqQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class PubCcsqAction extends BaseStruts3Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "tjsj desc"; 
	
	//forward paths
	protected static final String LIST_JSP= "/pub/pubCcsq_list.jsp";
	protected static final String CREATE_JSP = "/pub/pubCcsq_add.jsp";
	protected static final String EDIT_JSP = "/pub/pubCcsq_edit.jsp";
	protected static final String SH_JSP = "/pub/pubCcsq_sh.jsp";
	protected static final String SHOW_JSP = "/pub/pubCcsq_show.jsp";
	protected static final String DETAIL_JSP = "/pub/pubCcsq_detail.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pub/list.do";

	private static final String My_JSP = "/pub/pubCcsq_my.jsp";
	private static final String My_sh_JSP = "/task/pubCcsq_sh_list.jsp";
	
	private PubCcsqManager pubCcsqManager;
	
	private PubCcsq pubCcsq;
	java.lang.String id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			pubCcsq = new PubCcsq();
		} else {
			pubCcsq = (PubCcsq)pubCcsqManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setPubCcsqManager(PubCcsqManager manager) {
		this.pubCcsqManager = manager;
	}	
	
	public Object getModel() {
		return pubCcsq;
	}
	
	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		PubCcsqQuery query = newQuery(PubCcsqQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = pubCcsqManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	public String myList() {
		PubCcsqQuery query = newQuery(PubCcsqQuery.class,DEFAULT_SORT_COLUMNS);
		query.setUserid(getLoginInfo().getUserid());
		Page page = pubCcsqManager.findPage(query);
		savePage(page,query);
		return My_JSP;
	}
	
	public String myTask() {
		PubCcsqQuery query = newQuery(PubCcsqQuery.class,DEFAULT_SORT_COLUMNS);
		query.setSpr(getLoginInfo().getUserid());
		this.setOpt("ccsq");
		Page page = pubCcsqManager.myTask(query);
		savePage(page,query);
		return My_sh_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	public String detail() {
		return DETAIL_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		pubCcsq.setYgbh(getLoginInfo().getUsername());
		pubCcsq.setYgxm(getLoginInfo().getRealname());
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			pubCcsq.setYgbh(getLoginInfo().getUsername());
			pubCcsq.setYgxm(getLoginInfo().getRealname());
			pubCcsq.setUserid(getLoginInfo().getUserid());
			pubCcsq.setBm(getLoginInfo().getDeptname());
			pubCcsqManager.saveCc(pubCcsq);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	public String getSh() {
		return SH_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			pubCcsqManager.updateCc(this.pubCcsq,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	public void sh() {
		Map<String, Object> resInfo = null;
		try{
			pubCcsqManager.sh(this.pubCcsq);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			pubCcsqManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
	
	private String opt;

	public String getOpt() {
		return opt;
	}

	public void setOpt(String opt) {
		this.opt = opt;
	}
	
}
